package weaver.jobs.zhlx.duss.cron;

import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;

/**
 * @Title: ecology
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-08-29
 * @Description: 定时调用SAP成本中心接口同步供应商主数据到建模表单
 * @ModifyLog:
 **/

public class WeaClearWfDataCron extends BaseCronJob {

    @Override
    public void execute() {
        try {
            BaseBean baseBean = new BaseBean();
            baseBean.writeLog(">>>>>WeaClearWfDataCron>>>>>ExecuteStart=" + DateUtil.getFullDate());
            RecordSet rs = new RecordSet();
            rs.executeQuery("select id,workflowname,abs(formid) formid,subcompanyid from workflow_base where subcompanyid !=522 and nvl(wfdocdiaplaywatermark,'0') !='1' order by id desc");
            int counts = rs.getCounts();
            int succcount = 0;
            int failcount = 0;
            int count = 1;
            while (rs.next()) {
                long startTime = System.currentTimeMillis();
                String workflowid = Util.null2String(rs.getString("id"));
                String workflowname = Util.null2String(rs.getString("workflowname"));
                String formid = Util.null2String(rs.getString("formid"));
                String subcompanyid = Util.null2String(rs.getString("subcompanyid"));
                boolean status = deleteWfData(workflowid, formid,count);
                if (status) {
                    rs.executeUpdate("update workflow_base set wfdocdiaplaywatermark='1'  where id =?",workflowid);
                    succcount++;
                } else {
                    failcount++;
                }
                long endTime = System.currentTimeMillis();
                long totalTime = endTime - startTime;
                baseBean.writeLog("第"+count+"次删除执行的时间：" + totalTime + "毫秒");
                baseBean.writeLog(">>>>>WeaClearWfDataCron>>>>>counts=" + counts + ",succcount=" + succcount + ",failcount=" + failcount);
                baseBean.writeLog(">>>>>WeaClearWfDataCron>>>>>subcompanyid=" + subcompanyid + ",workflowid=" + workflowid + ",workflowname=" + workflowname
                        + ",formid=" + formid + ",status：" + status);
                count++;
            }
            baseBean.writeLog(">>>>>WeaClearWfDataCron>>>>>ExecuteEnd=" + DateUtil.getFullDate());
            baseBean.writeLog(">>>>>WeaClearWfDataCron>>>>总记录数：" + counts + ",成功记录数：" + succcount + ",失败记录数：" + failcount);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * @Description: 删除流程数据
     * @Param: [nodeid]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2023/03/21
     */
    public static boolean deleteWfData(String workflowid, String formid,int count) {
        BaseBean baseBean = new BaseBean();
        RecordSet rs = new RecordSet();
        boolean flag = false;
        long startTimeRequestbase = System.currentTimeMillis();
        boolean requestbaseFlag = rs.executeUpdate("delete from workflow_requestbase where workflowid=?", workflowid);
        long endTimeRequestbase = System.currentTimeMillis();
        long totalTimeRequestbase = endTimeRequestbase - startTimeRequestbase;
        baseBean.writeLog("第"+count+"次删除执行workflow_requestbase表的时间：" + totalTimeRequestbase + "毫秒");

        long startTimeRequestlog = System.currentTimeMillis();
        boolean requestlogFlag = rs.executeUpdate("delete from workflow_requestlog where workflowid=?", workflowid);
        long endTimeRequestlog = System.currentTimeMillis();
        long totalTimeRequestlog = endTimeRequestlog - startTimeRequestlog;
        baseBean.writeLog("第"+count+"次删除执行workflow_requestlog表的时间：" + totalTimeRequestlog + "毫秒");

        long startTimeCurrentoperator = System.currentTimeMillis();
        boolean currentoperatorFlag = rs.executeUpdate("delete from workflow_currentoperator where workflowid=?", workflowid);
        long endTimeCurrentoperator = System.currentTimeMillis();
        long totalTimeCurrentoperator = endTimeCurrentoperator - startTimeCurrentoperator;
        baseBean.writeLog("第"+count+"次删除执行workflow_currentoperator表的时间：" + totalTimeCurrentoperator + "毫秒");

        long startTimeRequestdeletelog = System.currentTimeMillis();
        boolean requestdeletelogFlag = rs.executeUpdate("delete from workflow_requestdeletelog where workflow_id=?", workflowid);
        long endTimeRequestdeletelog = System.currentTimeMillis();
        long totalTimeRequestdeletelog = endTimeRequestdeletelog - startTimeRequestdeletelog;
        baseBean.writeLog("第"+count+"次删除执行workflow_requestdeletelog表的时间：" + totalTimeRequestdeletelog + "毫秒");

        long startTimeOperatLog = System.currentTimeMillis();
        boolean requestoperatelogFlag = deleteOperatLog(formid);
        long endTimeOperatLog = System.currentTimeMillis();
        long totalTimeOperatLog = endTimeOperatLog - startTimeOperatLog;
        baseBean.writeLog("第"+count+"次删除执行workflow_requestoperatelog表的时间：" + totalTimeOperatLog + "毫秒");

        if (requestoperatelogFlag && requestbaseFlag && requestlogFlag && currentoperatorFlag && requestdeletelogFlag) {
            long startTimeFormtable = System.currentTimeMillis();
            flag = deleteDetail(formid);
            long endTimeFormtable = System.currentTimeMillis();
            long totalTimeFormtable = endTimeFormtable - startTimeFormtable;
            baseBean.writeLog("第"+count+"次删除执行formtable_main_"+formid+"表的时间：" + totalTimeFormtable + "毫秒");
        }
        return flag;
    }

    /**
     * @Description: 删除节点操作明细记录日志
     * @Param: [nodeid]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2023/03/21
     */
    public static boolean deleteOperatLog(String formid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select requestid from formtable_main_" + formid + " where 1=1");
        int counts = rs.getCounts();
        int count = 0;
        while (rs.next()) {
            String requestid = Util.null2String(rs.getString("requestid"));
            boolean flag = rs.executeUpdate("delete from workflow_requestoperatelog where requestid=?", requestid);
            if (flag) {
                count++;
            }
        }
        return counts == count;
    }

    /**
     * @Description: 删除明细
     * @Param: [nodeid]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2023/03/21
     */
    public static boolean deleteDetail(String formid) {
        RecordSet rs = new RecordSet();
        boolean flag = false;
        rs.executeQuery("select tablename from workflow_billdetailtable where abs(billid)=?", formid);
        int counts = rs.getCounts();
        int count = 0;
        while (rs.next()) {
            String tablename = Util.null2String(rs.getString("tablename"));
            boolean flag1 = rs.executeUpdate("truncate table " + tablename );
            if (flag1) {
                count++;
            }
        }
        if (counts == count) {
            flag = rs.executeUpdate("truncate table formtable_main_" + formid);
        }
        return flag;
    }


}

